package com.kotlin.android.simple.permission

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.kotlin.android.core.BaseVMActivity
import com.kotlin.android.core.BaseViewModel
import com.kotlin.android.ktx.ext.e
import com.kotlin.android.ktx.ext.openAppSetting
import com.kotlin.android.ktx.ext.permission.permissions
import com.kotlin.android.router.path.RouterActivityPath
import com.kotlin.android.simple.R
import kotlinx.android.synthetic.main.simple_permission.*


/**
 *
 * Created on 2020/6/8.
 *
 * @author o.s
 */
@Route(path = RouterActivityPath.Simple.PAGER_PERMISSION_VM)
class PermissionVMActivity : BaseVMActivity<BaseViewModel>() {
    override fun getLayoutResId() = R.layout.simple_permission

    override fun initVM(): BaseViewModel = viewModels<BaseViewModel>().value

    override fun initView() {
        simpleRequest1?.setOnClickListener {
            openAppSetting()
        }

        simpleRequest2?.setOnClickListener {
            locationP()
        }

        simpleRequest3?.setOnClickListener {
            someP()
        }

    }

    override fun initData() {
    }

    override fun startObserve() {
    }

    private fun sdcardP() {
        permissions(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ) {
            onShowRationale {
                "onShowRationale :: ${it.premissions}".e()
                request(it)
            }
            onDenied {
                "onDenied :: $it".e()
            }
            onGranted {
                "onGranted :: $it".e()
            }
            onNeverAskAgain {
                "onNeverAskAgain :: $it".e()
            }
        }
    }

    private fun locationP() {
        permissions(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        ) {
            onShowRationale {
                "onShowRationale :: ${it.premissions}".e()
                request(it)
            }
            onDenied {
                "onDenied :: $it".e()
            }
            onGranted {
                "onGranted :: $it".e()
            }
            onNeverAskAgain {
                "onNeverAskAgain :: $it".e()
            }
        }
    }

    private fun someP() {
        permissions(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CAMERA
        ) {
            onShowRationale {
                "onShowRationale :: ${it.premissions}".e()
                request(it)
            }
            onDenied {
                "onDenied :: $it".e()
            }
            onGranted {
                "onGranted :: $it".e()
            }
            onNeverAskAgain {
                "onNeverAskAgain :: $it".e()
            }
        }
    }
}